﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Common.Helper;
using System.Web;
using Exhibition.Dao.ViewModel;
using Exhibition.Common.Extension;
namespace Exhibition.Service
{
    public class ControlService
    {

        public HttpContext Context { get; private set; }
        public string PostData { get; set; }

        public string StrValue { get; set; }
        public ControlService(HttpContext context)
        {
            Context = context;

            if (Context.Request["strValue"] != null)
                StrValue = Context.Request["strValue"];
            if (Context.Request["PostData"] != null)
                PostData = Context.Request["PostData"];
        }

        public ControlService()
        {
        }

        /// <summary>
        /// 获取最新注册
        /// </summary>
        /// <returns></returns>
        public string GetTopReg()
        {
            int max = Convert.ToInt32(Context.Request["tpnum"]);
            int outnum = 0;
            List<UserModel> umdls = CenterService.userService.GetUserList("", 0, 0, 5, out outnum);
            return umdls.ToJsonItem();
        }
        /// <summary>
        /// 删除nav
        /// </summary>
        /// <returns></returns>
        public string DelNavId()
        {
            if (CenterService.navService.DelById(Convert.ToInt32(PostData)) > 0)
            {
                return "Success";
            }
            return "Failed";
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public string UserLogin()
        {
            return "";
        }
        /// <summary>
        /// 返回true 代表有此用户  false为可以注册
        /// </summary>
        /// <returns></returns>
        public string ChkUser()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                UserModel umd = CenterService.userService.GetByEMail(PostData);
                if (umd != null)
                {
                    return "true";
                }

            }
            return "false";
        }
        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        public string ChkCode()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                if (HttpContext.Current.Session != null)
                {
                    if (HttpContext.Current.Session["VerifyCode"].ToString().ToLower() == PostData.ToLower())
                    {
                        return "true";
                    }
                }
            }
            return "false";
        }
        /// <summary>
        /// 获取参展证
        /// </summary>
        /// <returns></returns>
        public string GetBadge()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                BadgesModel mdls = CenterService.badgesService.GetOneById(Convert.ToInt32(PostData));
                return mdls.ToJsonItem();
            }
            return "";
        }
        /// <summary>
        ///保存单条
        /// </summary>
        /// <returns></returns>
        public string SaveBadge()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                BadgesModel mdls = this.PostData.FromJsonTo<BadgesModel>();
                var curUser = MembershipService.GetUser();

                if (mdls != null)
                {
                    mdls.CreateTime = DateTime.Now;

                    if (CenterService.badgesService.Save(mdls) > 0)
                    {
                        return "Success";
                    }
                }
            }
            return "";
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public string DelBadge()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                var arry = PostData.Split(',');
                var blg = false;
                if (arry.Count() > 0)
                {
                    foreach (var s in arry)
                    {
                        CenterService.badgesService.DelById(Convert.ToInt32(s));
                    }
                }
                else
                {
                    CenterService.badgesService.DelById(Convert.ToInt32(PostData));
                }
                return "Success";
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 通用删除
        /// </summary>
        /// <returns></returns>
        public string DelCommData()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                if (!string.IsNullOrEmpty(StrValue))
                {
                    var rtn = CenterService.operatingDataService.PhyDeleteData(Convert.ToInt32(StrValue), PostData);
                    if (rtn)
                    {
                        return "Success";
                    }

                }
            }
            return "Failed";
        }

        /// <summary>
        /// 更新用户email
        /// </summary>
        /// <returns></returns>
        public string UpdateEmail() {
            if (!string.IsNullOrEmpty(PostData)) {
               var user= MembershipService.GetLoginUser();
               user.Email = PostData;
               if (CenterService.userService.Save(user) > 0) {
                   return "Success";
               }
            }
            return "Failed";
        }
        /// <summary>
        /// 更新电话
        /// </summary>
        /// <returns></returns>
        public string UpdatePhone() {
            if (!string.IsNullOrEmpty(PostData))
            {
                var user = MembershipService.GetLoginUser();
                user.Mobile = PostData;
                if (CenterService.userService.Save(user) > 0)
                {
                    return "Success";
                }
            }
            return "Failed";
        }

        /// <summary>
        /// 获取子区域
        /// </summary>
        /// <returns></returns>
        public string GetSonArea()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                List<AreaModel> areas = CenterService.areaService.GetListByPid(Convert.ToInt32(PostData));
                return areas.ToJsonItem();
            }
            return "";
        }

        public string SaveArea() {
            if (!string.IsNullOrEmpty(PostData))
            {
                var model = this.PostData.FromJsonTo<AreaModel>();
               // model.AreaEName = StringUtils.ConvertToChinese();
                if (model != null) {
                    if (CenterService.areaService.Save(model) > 0) {
                        return "Success";
                    }
                }
            }
            return "Failed";
        }

        /// <summary>
        /// 获取所有的区域
        /// </summary>
        /// <returns></returns>
        public string GetArea() {
            List<AreaModel> rtnarea = new List<AreaModel> ();
            List<AreaModel> areas = new List<AreaModel>();
            List<AreaModel> allareas = new List<AreaModel>();
            if (Context.Request["pid"] != null)
            {
                areas = CenterService.areaService.GetListByPid(Convert.ToInt32(Context.Request["pid"]));
            }
            if (Context.Request["id"] != null)
            {
                areas = CenterService.areaService.GetListByPid(Convert.ToInt32(Context.Request["id"]));
            }
            allareas = CenterService.areaService.GetAllArea();
           
            if (areas != null) {

                foreach (var area in areas) {

                    area.state = allareas.Where(s => s.ParentId == area.Id).Count() > 0 ? "closed" : "";
                    rtnarea.Add(area);
                 
                }
                return rtnarea.ToJsonItem();
            }
            return "";
        }


        /// <summary>
        /// 删除系统信息
        /// </summary>
        /// <returns></returns>
        public string DelMessage()
        {
            var uid = 0;
            var curid = 0;
            if (!string.IsNullOrEmpty(PostData))
            {
                curid = Convert.ToInt32(PostData);
            }
            if (!string.IsNullOrEmpty(StrValue))
            {
                uid = Convert.ToInt32(StrValue);
            }
            MessageInfoModel msg = CenterService.messageInfoService.GetOneById(curid);
            if (msg != null)
            {
                msg.DelUserId = msg.DelUserId + "," + uid;
            }

            if (CenterService.messageInfoService.Save(msg) > 0)
            {
                return "Success";
            }
            else
            {
                return "";
            }

        }



    }
}
